Toriality's Blog

oop

created_at:

June 4, 2024 at 5:40 PM

last_updated:

July 15, 2024 at 8:11 PM

Object-Oriented Programming Concepts

What is an Object?

Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gears, changing pedal cadence, applying brakes).

Software objects are conceptually similar. They store states in fields (variables) and exposes its behavior in methods (functions). Methods serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation.

Bundling code into individual software objects provide a number of benefits such as:

  1. Modularity: The source code for an object can be written and maintained independently of the source code for other objects.
  2. Information-hiding: By interacting only with the object's methods, the details of the internal implementation remain hidden from the outside world.
  3. Code Reusability: If an object already exists you can use that object in your program, allowing it to test/implement/debug task-specific things.
  4. De-bugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement.

What is a Class?

A class is a blueprint for creating objects. It defines the object's possible states and behaviors. The class is what the object is instantiated from.

What is Inheritance?

Different kinds of objects often have certain amount in common with each other.

Object-oriented programming allows for classes to inherit other classes.

What is an Interface?

In the most common form, an interface is a group of related methods with empty bodeis.

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

What is a Package?

A package is a namespace that organizes a set of related classes and interfaces. Conceptually think of packages as being similar to folders on your computer.